package com.killer.listener;

import com.killer.common.entity.Result;
import com.killer.goods.entity.Spu;
import com.killer.goods.feign.SpuClient;
import com.killer.auth.service.IndexService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@RocketMQMessageListener(topic = "goods_update",consumerGroup = "goods-service")
@Slf4j
public class GoodsUpdateListener implements RocketMQListener<Long> {

    @Autowired
    private IndexService indexService;
    @Autowired
    private SpuClient spuClient;

    @Override
    public void onMessage(Long id) {
        log.info("收到spu更新通知消息，id是:{}", id);
        //拼接url
        Result<Spu> result = spuClient.findById(id);
        if (result == null||result.getData()==null) {
            return;
        }
        Spu spu=result.getData();
        if (spu.getIsMarketable() == false) {
            try {
                indexService.removeGoodsBySupId(id);
            } catch (IOException e) {
                log.error("删除索引失败,spuId:"+id, e);
            }
        }else{
            indexService.importGoodsBySpuId(id);
        }
    }

}
